import useSForm from "@/components/public/extForm/useSForm";


function useSModal(title,formDefaultValue={},
                   api={}){

    const modalShow = ref(false)
    const modalLoading = ref(false)

    const {editApi,addApi,queryApi,deleteApi,batchDeleteApi,getInfoById} = api

    let sForm = useSForm(formDefaultValue,api);

    /**
     * 新增
     */
    const onAdd = () => {
        modalShow.value = true
        modalLoading.value = false
        sForm.onAdd();
    }
    /**
     * 编辑
     * @param row
     * @param id
     */
    const onEdit = (row ,id) => {
        modalShow.value = true
        modalLoading.value = false
        sForm.onEdit(row,id);
    }
    /**
     * 保存
     * 返回 Promise，解决函数执行完后需要续写逻辑的需求
     */
    const onSubmit = () => {
        return new Promise((resolve,reject)=>{
            sForm.onSubmit().then(res=>{
                modalShow.value = false
                modalLoading.value = true
                resolve(res)
            })
        })
    }
    const loadRules = (d) => {
        return sForm.loadRules(d)
    }
    const onReset = () => {
        return sForm.onReset()
    }
    const  {formValue, formRef,formActionType} = sForm;
    return {
        onAdd,
        onEdit,
        onSubmit,
        formValue,
        formRef,
        loadRules,
        onReset,
        modalShow,
        modalLoading,
        modalTitle: computed(() => {
            return formActionType.value + title
        }),
    }
}

export default useSModal;